/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Dao;

import Entity.Director;
import Entity.Movie;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.Query;

/**
 *
 * @author Leoz
 */
public class movieDao {

    public List<Movie> searchbyName(String name, EntityManager em) throws NoResultException {
        Query q = em.createNamedQuery("Movie.findByName").setParameter("name", name);
        List<Movie> movies = q.getResultList();
        return movies;
    }

    public List<Movie> searchbyGenre(String genre, EntityManager em) throws NoResultException {
        Query q = em.createNamedQuery("Movie.findByGenre").setParameter("genre", genre);
        List<Movie> movies = q.getResultList();
        return movies;
    }

    public List<Movie> searchbyDirector(long directorId, EntityManager em) {
        Query q = em.createNativeQuery("Select m.* from Movie m,Director d where d.idDirector =#idDirector  AND d.idDirector = m.Director_idDirector", Movie.class).setParameter("idDirector", directorId);
        List<Movie> movies = q.getResultList();
        return movies;
    }
    public List<Movie> searchbyActor(long actorId, EntityManager em) {
        Query q = em.createNativeQuery("Select m.* from Movie m, Actor a, Actor_has_Movie am WHERE a.idActor = #idActor and am.Actor_idActor = a.idActor and am.Movie_idMovie = m.idMovie", Movie.class).setParameter("idActor", actorId);
        List<Movie> movies = q.getResultList();
        return movies;
    }
    
}
